<?php

namespace app\index\model;

use think\Model;
use think\facade\Session;

use app\index\validate\Reply as Validate;

class Reply extends Model
{
    protected $insert = ['user_id'];

    protected static function init()
    {
        self::observe(\app\index\observer\Reply::class);
    }

    // belongs to user
    public function user()
    {
        return $this->belongsTo('User');
    }

    // belongs to category
    public function topic()
    {
        return $this->belongsTo('Topic');
    }

    protected function setUserIdAttr()
    {
        $current_user = User::currentUser();
        if(empty($current_user)){
            return 0;
        }else{
            return $current_user->id;
        }
    }

    /**
     * 创建评论
     * @Author   zhanghong(Laifuzi)
     * @DateTime 2019-02-25
     * @param    array              $data 表单提交数据
     * @return   Object                   FunctionResult
     */
    static public function createItem($data)
    {
        $res = new FunctionResult('创建评论记录失败');

        $validate = new Validate;
        if(!$validate->scene('create_or_update')->check($data)){
            $res->fail($validate->getError(), []);
            return $res;
        }

        $reply = new self;
        $reply->save($data);

        $res->success('创建评论记录成功', ['id' => $reply->id]);
        return $res;
    }

    /**
     * 自定义查询方法
     * @Author   zhanghong(Laifuzi)
     * @DateTime 2019-02-26
     * @param    array              $param    请求参数
     * @param    integer            $per_page 每页显示记录数量
     * @return   Pagination                   分页查询结果
     */
    static public function minePaginate($param, $per_page = 10)
    {

        $self = self::with('user,topic')->order('id', 'DESC');

        foreach ($param as $name => $value) {
            switch ($name) {
                case 'topic_id':
                case 'user_id':
                    if($value > 0){
                        $self = $self->where($name, $value);
                    }
                    break;
            }
        }
        $paginate = $self->paginate($per_page);
        return $paginate;
    }

    /**
     * 删除评论记录
     * @Author   zhanghong(Laifuzi)
     * @DateTime 2019-02-20
     * @param    integer            $id   Topic ID
     * @param    array              $data 表单提交数据
     * @return   Object                   FunctionResult
     */
    static public function deleteItem($id, $data)
    {
        $res = new FunctionResult('删除评论记录失败');

        $reply = self::find($id);
        if(empty($reply)){
            $res->success('评论记录已删除', []);
            return $res;
        }

        return $reply->deleteInfo($data);
    }

    /**
     * 判断用户是否可以删除评论记录
     * @Author   zhanghong(Laifuzi)
     * @DateTime 2019-02-27
     * @param    User               $user User Item
     * @return   Object                   FunctionResult
     */
    public function canDelete($user = NULL)
    {
        $res = new FunctionResult('删除评论记录信息失败');

        if(is_null($user)){
            $user = User::currentUser();
        }

        if(empty($user)){
            $res->fail('请先登录系统');
            return $res;
        }

        $check_topic = $this->topic->canDelete();

        if(!$check_topic->status && $this->user_id != $user->id){
            $res->fail('对不起，您没有权限删除当前评论记录');
            return $res;
        }

        $res->success('可以删除');
        return $res;
    }

    /**
     * 删除记录
     * @Author   zhanghong(Laifuzi)
     * @DateTime 2019-02-27
     * @param    array              $data 表单提交数据
     * @return   Object                   FunctionResult
     */
    public function deleteInfo($data)
    {
        $res = new FunctionResult('删除评论记录信息失败');

        if(!is_array($data)){
            $data = [];
        }
        $data['id'] = $this->id;

        $validate = new Validate;
        if(!$validate->scene('delete')->check($data)){
            $res->fail($validate->getError());
            return $res;
        }

        $check_res = $this->canDelete();
        if(!$check_res->status){
            return $check_res;
        }

        $this->delete();
        $res->success('删除评论记录成功', []);
        return $res;
    }
}
